// 1、无头单向非循环链表实现
public class SingleLinkedList {
    //加static生成对象不依赖于外部对象
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表当前节点的头结点引用
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
        }
        while(cur != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index == 0){
            addFirst(data);
        }
        if(index == getSize()){
            addLast(data);
        }
        checkIndex(index);
        ListNode node = new ListNode(data);
        ListNode cur = head;
        for(int i=0;i<index;i++){
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //获取链表长度
    public int getSize(){
        ListNode cur = head;
        int len = 0;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }
    //检查index的合法性
    public void checkIndex(int index) {
        if(index <0 || index > getSize()){
            throw new indexOutOfException("index的位置不合法！");
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            if(cur.val == key){
                return true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return; //一个节点都没有
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = searchPre(key);
        if(cur == null){
            System.out.println("没有这个节点");
            return;
        }
        cur.next = cur.next.next;
    }
    //找到关键字key的前一个节点
    private ListNode searchPre(int key){
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null; //没有要删除的节点
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
        }
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public void clear(){
        head = null;
    }
}
